package com.medicine.hbp.ui;

import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;

import com.medicine.hbp.R;
import com.medicine.hbp.adapter.InterestTwoAdapter;
import com.medicine.hbp.adapter.TheDiseaseTypesAdapter;
import com.medicine.hbp.base.BaseActivity;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.InterestVo;
import com.medicine.hbp.mob.TheDiseaseTypesListVo;
import com.medicine.hbp.sp.LoginSp;
import com.medicine.hbp.uitl.NetWorkUtils;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.xListView.XListView;

/**
 * 病种选择
 *
 * @author Administrator
 *
 */
public class TheDiseaseTypesActivity extends BaseActivity {
	@BindView(id = R.id.xListView, click = false)
	public ListView xListView;
	@BindView(id = R.id.image_no_wifi) public ImageView NoNetWork;
	private TheDiseaseTypesAdapter adapter;

	@Override
	public void setRootView() {
		super.setRootView();
		setContentView(R.layout.activity_thediseasetypes);
	}

	@Override
	public void initWidget() {
		super.initWidget();
		initTop();
		adapter = new TheDiseaseTypesAdapter(aty);
		xListView.setAdapter(adapter);
		if(NetWorkUtils.getAPNType(aty)==0){
			xListView.setVisibility(View.GONE);
			NoNetWork.setVisibility(View.VISIBLE);
		}else{
			xListView.setVisibility(View.VISIBLE);
			NoNetWork.setVisibility(View.GONE);
			httpTheDiseaseTypes();
		}
		NoNetWork.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(NetWorkUtils.getAPNType(aty)==0){
					xListView.setVisibility(View.GONE);
					NoNetWork.setVisibility(View.VISIBLE);
					ToastUtil.showShort(aty, "断网啦！");
				}else{
					xListView.setVisibility(View.VISIBLE);
					NoNetWork.setVisibility(View.GONE);
					httpTheDiseaseTypes();
				}
			}
		});
	}

	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
	}
	/**
	 * 病种获取
	 */
	private void httpTheDiseaseTypes() {
		HttpParams params = new HttpParams();
		params.put("userId", LoginSp.getUserId(aty));
		AppHttps.HttpPost(this, HttpInterface.FIND_BLABER_BY_USER, params,
				new AppHttpCallBack<TheDiseaseTypesListVo>(TheDiseaseTypesListVo.class, aty) {
					@Override
					public void onSuccess(TheDiseaseTypesListVo result) {
						adapter.setItems(result.info);
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
				});
	}
	/**
	 * 病种保存
	 */
	private void httpbcTheDiseaseTypes() {
		String id= adapter.getid();
		HttpParams params = new HttpParams();
		params.put("markId", LoginSp.getUserId(aty));
		params.put("bLabers", id);//病种ID集合(以逗号间隔)
		AppHttps.HttpPost(this, HttpInterface.SAVE_BLABER, params,
				new AppHttpCallBack<Object>(Object.class, aty) {
					@Override
					public void onSuccess(Object result) {
						finish();
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
				});
	}
	@Override
	protected void onStop() {
		super.onStop();

	}
	private void initTop() {
		TopView topview = (TopView) findViewById(R.id.topview);
		topview.setTitle("请选择您要订阅的病种");
		topview.setLeftVisibility(View.GONE);
		topview.setLeftVisibilityExit(View.VISIBLE);
		topview.setRightVisibility(View.INVISIBLE);
		topview.setLeftOnClickExit(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				httpbcTheDiseaseTypes();

			}
		});
	}

}
